package com.ccx.coachsite.ui.softmgr;

import java.util.HashMap;

import android.content.Context;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;

import com.ccx.coachsite.R;
import com.ccx.coachsite.downloadmgr.DownloadService;
import com.ccx.coachsite.ui.CommonAppView;

public class SoftMgrDownloadMainView extends CommonAppView{

	/*---------------------控件区------------------------------*/
	private CheckedTextView ctv_downloading;
	private CheckedTextView ctv_downloaded;
	private LinearLayout ll_container;
	
	/*----------------其他变量-----------------------------*/	
	private SoftMgrDownloadingView downloadingView;
	private SoftMgrDownloadedView downloadedView;
	
	/*----------------公共变量-----------------------------*/
	private Context mContext;
	private CheckedTextView mDefaultSelectedView;//默认选中的标签卡

	public SoftMgrDownloadMainView(Context paramContext) {
		super(paramContext);
		
		mContext=paramContext;
		//initView();
	}
	
	public void initView()
	{
		addView(R.layout.download_mgr_main_view);
		
		ctv_downloading=(CheckedTextView) findViewById(R.id.ctv_downloading);
		ctv_downloaded=(CheckedTextView) findViewById(R.id.ctv_downloaded);
		ll_container=(LinearLayout) findViewById(R.id.ll_container);
		
		HashMap<String, Object> initParaMap = new HashMap<String, Object>();	
		
		
		//初始默认显示的页面：正在下载或者已下载界面
		//if(DownloadService.hasDownloadingTask())
		if(false)
		{
			//有正在下载的任务，则默认显示的是正在下载的页面
			downloadingView = new SoftMgrDownloadingView(mContext, initParaMap);
			
			mDefaultSelectedView=ctv_downloading;
			changeMainView(downloadingView);
		}else{
			//显示已下载界面
			downloadedView = new SoftMgrDownloadedView(mContext, initParaMap);
			
			mDefaultSelectedView=ctv_downloaded;
			changeMainView(downloadedView);
		}
		mDefaultSelectedView.setChecked(true);	
		NavMenuClickListener navMenuClick=new NavMenuClickListener();
		ctv_downloading.setOnClickListener(navMenuClick);
		ctv_downloaded.setOnClickListener(navMenuClick);
	}
	
	private void changeMainView(View targetView)
    {
    	LayoutParams param = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
    	targetView.setLayoutParams(param);
    	ll_container.removeAllViews();
    	ll_container.addView(targetView);
    }
	
	
	/*---------------------内部类区------------------------------*/
	/**
     * 监听类
     * 头部菜单的点击事件监听
     */
    private class NavMenuClickListener implements OnClickListener{

    	private CheckedTextView mOldSelectedView=mDefaultSelectedView; //默认选中的标签卡
    	@Override
    	public void onClick(View v) {
    		
    		//点中的菜单与之前选中菜单不是同一个
    		if(mOldSelectedView!=null&&mOldSelectedView.getId()!=v.getId())
    		{
    			mOldSelectedView.setChecked(false);
    			//设置点中的菜单为选中的
        		((CheckedTextView)v).setChecked(true);
    			//记录选中的菜单，供下次点击菜单时进行状态切换
        		mOldSelectedView=((CheckedTextView)v);
        		
        		/*-------------跳转Activity--------------*/
        		if ( v.getId()==R.id.ctv_downloading ) {
        			
        			if (downloadingView==null)
        				downloadingView = new SoftMgrDownloadingView(mContext, null);
        			
        			changeMainView(downloadingView);
        		}
        		
        		if ( v.getId()==R.id.ctv_downloaded ) {
        			if (downloadingView==null)
        				downloadedView = new SoftMgrDownloadedView(mContext, null);
        			
        			changeMainView(downloadedView);
        		}         			
    		}
    		
    	}
    	
    } //end NavMenuClickListener
    
    @Override
    public void clearCache() {
    	// TODO Auto-generated method stub
    	super.clearCache();
    	if (downloadingView!=null)
    		downloadingView.clearCache();
    	if (downloadedView!=null)
    		downloadedView.clearCache();
    }
}
